using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace Impact.Model.Objects
{
	/// <summary>
	/// Strongly-typed collection for the Tag class.
	/// </summary>
    [Serializable]
	public partial class TagCollection : ActiveList<Tag, TagCollection>
	{	   
		public TagCollection() {}
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the impact_Tags table.
	/// </summary>
	[Serializable]
	public partial class Tag : ActiveRecord<Tag>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public Tag()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public Tag(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public Tag(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public Tag(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("impact_Tags", TableType.Table, DataService.GetInstance("ImpactProvider"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarTagId = new TableSchema.TableColumn(schema);
				colvarTagId.ColumnName = "TagId";
				colvarTagId.DataType = DbType.Int32;
				colvarTagId.MaxLength = 0;
				colvarTagId.AutoIncrement = true;
				colvarTagId.IsNullable = false;
				colvarTagId.IsPrimaryKey = true;
				colvarTagId.IsForeignKey = false;
				colvarTagId.IsReadOnly = false;
				colvarTagId.DefaultSetting = @"";
				colvarTagId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarTagId);
				
				TableSchema.TableColumn colvarTextX = new TableSchema.TableColumn(schema);
				colvarTextX.ColumnName = "Text";
				colvarTextX.DataType = DbType.AnsiString;
				colvarTextX.MaxLength = 50;
				colvarTextX.AutoIncrement = false;
				colvarTextX.IsNullable = false;
				colvarTextX.IsPrimaryKey = false;
				colvarTextX.IsForeignKey = false;
				colvarTextX.IsReadOnly = false;
				colvarTextX.DefaultSetting = @"";
				colvarTextX.ForeignKeyTableName = "";
				schema.Columns.Add(colvarTextX);
				
				TableSchema.TableColumn colvarCreatedBy = new TableSchema.TableColumn(schema);
				colvarCreatedBy.ColumnName = "CreatedBy";
				colvarCreatedBy.DataType = DbType.String;
				colvarCreatedBy.MaxLength = 50;
				colvarCreatedBy.AutoIncrement = false;
				colvarCreatedBy.IsNullable = false;
				colvarCreatedBy.IsPrimaryKey = false;
				colvarCreatedBy.IsForeignKey = false;
				colvarCreatedBy.IsReadOnly = false;
				colvarCreatedBy.DefaultSetting = @"";
				colvarCreatedBy.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreatedBy);
				
				TableSchema.TableColumn colvarCreatedOn = new TableSchema.TableColumn(schema);
				colvarCreatedOn.ColumnName = "CreatedOn";
				colvarCreatedOn.DataType = DbType.DateTime;
				colvarCreatedOn.MaxLength = 0;
				colvarCreatedOn.AutoIncrement = false;
				colvarCreatedOn.IsNullable = false;
				colvarCreatedOn.IsPrimaryKey = false;
				colvarCreatedOn.IsForeignKey = false;
				colvarCreatedOn.IsReadOnly = false;
				colvarCreatedOn.DefaultSetting = @"";
				colvarCreatedOn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreatedOn);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["ImpactProvider"].AddSchema("impact_Tags",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("TagId")]
		[Bindable(true)]
		public int TagId 
		{
			get { return GetColumnValue<int>(Columns.TagId); }
			set { SetColumnValue(Columns.TagId, value); }
		}
		  
		[XmlAttribute("Text")]
		[Bindable(true)]
		public string Text 
		{
			get { return GetColumnValue<string>(Columns.Text); }
			set { SetColumnValue(Columns.Text, value); }
		}
		  
		[XmlAttribute("CreatedBy")]
		[Bindable(true)]
		public string CreatedBy 
		{
			get { return GetColumnValue<string>(Columns.CreatedBy); }
			set { SetColumnValue(Columns.CreatedBy, value); }
		}
		  
		[XmlAttribute("CreatedOn")]
		[Bindable(true)]
		public DateTime CreatedOn 
		{
			get { return GetColumnValue<DateTime>(Columns.CreatedOn); }
			set { SetColumnValue(Columns.CreatedOn, value); }
		}
		
		#endregion
		
		
		#region PrimaryKey Methods		
		
        protected override void SetPrimaryKey(object oValue)
        {
            base.SetPrimaryKey(oValue);
            
            SetPKValues();
        }
        
				
		private Impact.Model.Objects.ArticleTagCollection colArticleTags;
		public Impact.Model.Objects.ArticleTagCollection ArticleTags
		{
			get
			{
				if(colArticleTags == null)
				{
					colArticleTags = new Impact.Model.Objects.ArticleTagCollection().Where(ArticleTag.Columns.TagId, TagId).Load();
					colArticleTags.ListChanged += new ListChangedEventHandler(colArticleTags_ListChanged);
				}
				return colArticleTags;			
			}
			set 
			{ 
					colArticleTags = value; 
					colArticleTags.ListChanged += new ListChangedEventHandler(colArticleTags_ListChanged);
			}
		}
		
		void colArticleTags_ListChanged(object sender, ListChangedEventArgs e)
		{
		    if (e.ListChangedType == ListChangedType.ItemAdded)
		    {
		        // Set foreign key value
		        colArticleTags[e.NewIndex].TagId = TagId;
		    }
		}
		
		#endregion
		
			
		
		//no foreign key tables defined (0)
		
		
		
		#region Many To Many Helpers
		
		 
		public Impact.Model.Objects.ArticleCollection GetArticleCollection() { return Tag.GetArticleCollection(this.TagId); }
		public static Impact.Model.Objects.ArticleCollection GetArticleCollection(int varTagId)
		{
		    SubSonic.QueryCommand cmd = new SubSonic.QueryCommand("SELECT * FROM [dbo].[impact_Articles] INNER JOIN [impact_ArticleTags] ON [impact_Articles].[ArticleId] = [impact_ArticleTags].[ArticleId] WHERE [impact_ArticleTags].[TagId] = @TagId", Tag.Schema.Provider.Name);
			cmd.AddParameter("@TagId", varTagId, DbType.Int32);
			IDataReader rdr = SubSonic.DataService.GetReader(cmd);
			ArticleCollection coll = new ArticleCollection();
			coll.LoadAndCloseReader(rdr);
			return coll;
		}
				
		#endregion
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varTextX,string varCreatedBy,DateTime varCreatedOn)
		{
			Tag item = new Tag();
			
			item.Text = varTextX;
			
			item.CreatedBy = varCreatedBy;
			
			item.CreatedOn = varCreatedOn;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varTagId,string varTextX,string varCreatedBy,DateTime varCreatedOn)
		{
			Tag item = new Tag();
			
				item.TagId = varTagId;
			
				item.Text = varTextX;
			
				item.CreatedBy = varCreatedBy;
			
				item.CreatedOn = varCreatedOn;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn TagIdColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn TextXColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn CreatedByColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        public static TableSchema.TableColumn CreatedOnColumn
        {
            get { return Schema.Columns[3]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string TagId = @"TagId";
			 public static string Text = @"Text";
			 public static string CreatedBy = @"CreatedBy";
			 public static string CreatedOn = @"CreatedOn";
						
		}
		#endregion
		
		#region Update PK Collections
		
        public void SetPKValues()
        {
                if (colArticleTags != null)
                {
                    foreach (Impact.Model.Objects.ArticleTag item in colArticleTags)
                    {
                        if (item.TagId != TagId)
                        {
                            item.TagId = TagId;
                        }
                    }
               }
		}
        #endregion
    
        #region Deep Save
		
        public void DeepSave()
        {
            Save();
		}
        #endregion
	}
}
